package com.dc.free.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dc.free.common.base.BaseController;
import com.dc.free.util.IOUtil;
import com.dc.free.util.MapUtil;
import com.dc.free.util.WebUtil;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;


public class TestController extends BaseController {
	
	public static final String APP_ID = "wx577ec0fe2f5c9094";
	public static final String TOKEN_URL = "http://develop.zsgd.com/auth?cmd=getMpAccessToken&appid=wx577ec0fe2f5c9094&token=Jh2044695";
	public static final String TICKET_URL = "http://develop.zsgd.com/auth?cmd=getMpJSTicket&appid=wx577ec0fe2f5c9094&token=Jh2044695";
	public static final String IMAGE_URL = "https://api.weixin.qq.com/cityservice/face/identify/getimage?access_token=ACCESS_TOKEN";
	public static final String INFO_URL = "https://api.weixin.qq.com/cityservice/face/identify/getinfo?access_token=ACCESS_TOKEN";
	
	private static String getToken() {
		return JSON.parseObject(HttpKit.get(TOKEN_URL)).getString("data");
	}
	
	private static String getTicket() {
		return JSON.parseObject(HttpKit.get(TICKET_URL)).getString("data");
	}
	
	public void index() {
		renderJson("hello");
	}
	
	public void information() {
		render("../information.html");
	}
	
	public void save() {
		String name = getPara("name");
		String card = getPara("card");
		
		if(!StrKit.notBlank(name, card)) {
			setAttr("code", "1");
			setAttr("msg", "请填写姓名或身份证");
			renderJson();
			return;
		}
		
		setSessionAttr("name", name);
		setSessionAttr("card", card);
		
		setAttr("code", "0");
		setAttr("msg", "成功");
		renderJson();
	}
	
	public void verify() {
		String name = getSessionAttr("name");
		String card = getSessionAttr("card");
		
		if(!StrKit.notBlank(name, card)) {
			name = getPara("name");
			card = getPara("card");
		}
		
		if(!StrKit.notBlank(name, card)) {
			forwardAction("/test/information");
			return;
		}
		
		String ticket = getTicket();
		String url = WebUtil.getUrlWithParameter(getRequest());
		String noncestr = "dyci";
		String timestamp = new Date().getTime() + "";
		Map<String, Object> map = new TreeMap<String, Object>();
		map.put("noncestr", noncestr);
		map.put("jsapi_ticket", ticket);
		map.put("timestamp", timestamp);
		map.put("url", url);
		String sha1 = HashKit.sha1(MapUtil.buildUrlWithQuerySequenceString(map, false));
		
		setAttr("appId", APP_ID);
		setAttr("timestamp", timestamp);
		setAttr("nonceStr", noncestr);
		setAttr("signature", sha1);
		setAttr("name", name);
		setAttr("card", card);
		setAttr("success_url", getPara("success_url"));
		setAttr("error_url", getPara("error_url"));
		
		render("../verify.html");
	}
	
	public void handler() throws FileNotFoundException, IOException {
		String identifier = getPara("identifier");
		String name = getSessionAttr("name");
		String card = getSessionAttr("card");
		//XXIzTtMqCxwOaawoE91+VJvIYrho95KrESQ6tyXLge32ZDn3EPScHd2YZlX6yqFJSwZ8+maJIoIZwPIVBQWfsywoJzjta45dNsqOgkad8c7eg6YBlhhJ/d1quUa2FpAg
		//XXIzTtMqCxwOaawoE91+VJvIYrho95KrESQ6tyXLge2Q5JnqaRIxCDhKe9zsaYTcwznZLmKi549zQ1r2NNZk2Jbnud5+S6ACKRe8iKV1K9JSSvtrGt0XJ0jxlExjoyYh
		
		if(StrKit.isBlank(identifier)) {
			setAttr("code", "1");
			setAttr("msg", "验证失败");
			renderJson();
			return;
		}
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("verify_result", identifier);
		String mapStr = JSON.toJSONString(map);
		
		String info = HttpKit.post(INFO_URL.replace("ACCESS_TOKEN", getToken()), mapStr);
		JSONObject infoJson = JSON.parseObject(info);
		if(!HashKit.md5(name).equals(infoJson.getString("name_utf8_md5")) ||
				!HashKit.md5(card).equals(infoJson.getString("id_card_number_md5"))) {
			setAttr("code", "2");
			setAttr("msg", "身份信息不一致");
			renderJson();
			return;
		}
		
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		InputStream is = HttpUtils.download(IMAGE_URL.replace("ACCESS_TOKEN", getToken()), mapStr);
		FileOutputStream os = new FileOutputStream(new File("D:/image/" + name + sdf.format(new Date()) + ".jpg"));
		IOUtil.write(is, os);
		IOUtil.close(is);
		IOUtil.close(os);
		
		setAttr("code", "0");
		setAttr("msg", "认证成功");
		renderJson();
		
	}
	
}
